package com.example.testdemo.recyclerview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Scroller;

/**
 * description: 视频列表播放自定义recyclerview 模仿qq音乐视频播放
 *
 * @author zwx
 * @date 2019/8/29.
 */
public class VideoRecyclerView extends RecyclerView {
    private Context context;

    private int childHeight;
    private Scroller mScroller;

    private LinearLayoutManager manager;
    public VideoRecyclerView(Context context) {
        super(context);
//        init(context);
    }

    public VideoRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public VideoRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
//        init(context);
    }

    private int oldY;

    private void init(Context context){
        mScroller = new Scroller(context);
        setOnFlingListener(new OnFlingListener() {
            @Override
            public boolean onFling(int velocityX, int velocityY) {
                if(isUpMove){
                    int pos = manager.findFirstVisibleItemPosition();
//                        smoothScrollToPosition(pos);
                    smoothScrollBy(0, manager.findViewByPosition(pos).getBottom());

                }else {
                    smoothScrollToPosition(manager.findFirstVisibleItemPosition());
                }
                return true;
            }
        });
//        addOnScrollListener(new OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                super.onScrollStateChanged(recyclerView, newState);
//
//            }
//
//            @Override
//            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//                super.onScrolled(recyclerView, dx, dy);
//                Log.e("event", " onScrolled  y="+ dy);
//                if(isUp && Math.abs(oldY - dy) > 20){
//                    //强制停止滑动
//                    dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_CANCEL, 0, 0, 0));
//
//
//                    smoothScrollToPosition(manager.findFirstVisibleItemPosition());
//                }else{
//
//                }
//            }
//        });
    }

    @Override
    public void setLayoutManager(LayoutManager layout) {
        super.setLayoutManager(layout);
        manager = (LinearLayoutManager) getLayoutManager();

    }

    private float startX, startY;
    private float moveX, moveY;
    private float endX, endY;

    private boolean isUpMove;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                startX = ev.getX();
                startY = ev.getY();
                Log.e("event", " down  y="+ startY);


                break;
            case MotionEvent.ACTION_MOVE:

                if(ev.getY() < moveY){
                    isUpMove = true;
                }else{
                    isUpMove = false;
                }

                moveX = ev.getX();
                moveY = ev.getY();
                Log.e("event", " move  y="+ moveY);

                break;
            case MotionEvent.ACTION_UP:
                endX = ev.getX();
                endY = ev.getY();
                Log.e("event", " up  y="+ endY);


                break;
            default:
                break;
        }
        return super.dispatchTouchEvent(ev);
    }



    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {


        return super.onInterceptTouchEvent(e);
    }



    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
        super.computeScroll();

    }

}
